{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-18T11:42:19.318493Z",
     "start_time": "2025-09-18T11:42:17.361941Z"
    }
   },
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "import hmac\n",
    "import hashlib\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "base_url = \"https://api.sandbox.gemini.com\"\n",
    "endpoint = \"/v1/order/new\"\n",
    "url = base_url + endpoint\n",
    "\n",
    "gemini_api_key = \"account-zmidXEwP72yLSSybXVvn\"\n",
    "gemini_api_secret = \"375b97HfE7E4tL8YaP3SJ239Pky9\".encode()\n",
    "\n",
    "t = datetime.datetime.now()\n",
    "payload_nonce = str(int(time.mktime(t.timetuple())*1000))\n",
    "\n",
    "payload = {\n",
    "   \"request\": \"/v1/order/new\",\n",
    "   \"nonce\": payload_nonce,\n",
    "   \"symbol\": \"btcusd\",\n",
    "   \"amount\": \"5\",\n",
    "   \"price\": \"3633.00\",\n",
    "   \"side\": \"buy\",\n",
    "   \"type\": \"exchange limit\",\n",
    "   \"options\": [\"maker-or-cancel\"]\n",
    "}\n",
    "\n",
    "encoded_payload = json.dumps(payload).encode()\n",
    "b64 = base64.b64encode(encoded_payload)\n",
    "signature = hmac.new(gemini_api_secret, b64, hashlib.sha384).hexdigest()\n",
    "\n",
    "request_headers = {\n",
    "    'Content-Type': \"text/plain\",\n",
    "    'Content-Length': \"0\",\n",
    "    'X-GEMINI-APIKEY': gemini_api_key,\n",
    "    'X-GEMINI-PAYLOAD': b64,\n",
    "    'X-GEMINI-SIGNATURE': signature,\n",
    "    'Cache-Control': \"no-cache\"\n",
    "}\n",
    "\n",
    "response = requests.post(url,\n",
    "                         data=None,\n",
    "                         headers=request_headers)\n",
    "\n",
    "new_order = response.json()\n",
    "print(new_order)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'order_id': '73798698679683127', 'id': '73798698679683127', 'symbol': 'btcusd', 'exchange': 'gemini', 'avg_execution_price': '0.00', 'side': 'buy', 'type': 'exchange limit', 'timestamp': '1758195742', 'timestampms': 1758195742000, 'is_live': True, 'is_cancelled': False, 'is_hidden': False, 'was_forced': False, 'executed_amount': '0', 'client_order_id': '', 'options': ['maker-or-cancel'], 'price': '3633.00', 'original_amount': '5', 'remaining_amount': '5'}\n"
     ]
    }
   ],
   "execution_count": 1
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
